import { createRouter, createWebHistory } from 'vue-router';
import LoginRegister from '../views/LoginRegister.vue';

const handleLazyLoadError = (componentName) => (error) => {
  console.error(`Failed to load ${componentName} component:`, error);
  return LoginRegister;
};

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginRegister,
    meta: { title: '系统登录' }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue').catch(handleLazyLoadError('Home')),
    meta: { title: '首页', requiresAuth: true }
  },
  {
    path: '/guanliyuan/home',
    name: 'AdminHome',
    component: () => import('../views/guanliyuan/guanliHome.vue').catch(handleLazyLoadError('AdminHome')),
    meta: { title: '管理员首页', requiresAuth: true, role: 'admin' },
    children: [
      {
        path:'/guanliyuan/paibanyulan',
        component: () => import('../views/guanliyuan/paibanyulan.vue').catch(handleLazyLoadError('guanliInfo'))
      },
      {
        path: '/guanliyuan/keshiguanli',
        name: 'guanliInfo',
        component: () => import('../views/guanliyuan/keshiguanli.vue').catch(handleLazyLoadError('guanliInfo')),
      },
      {
        path: '/guanliyuan/paibanguanli',
        name: 'guanliChangePwd',
        component: () => import('../views/guanliyuan/paibanguanli.vue').catch(handleLazyLoadError('guanliChangePwd')),
      },
      {
        path: '/guanliyuan/yonghuguanli',
        name: 'guanlijiezhen',
        component: () => import('../views/guanliyuan/yonghuguanli.vue').catch(handleLazyLoadError('guanlijiezhen')),
      },
      {
        path: '/guanliyuan/yishenguanli',
        name: 'yishenguanli',
        component: () => import('../views/guanliyuan/yishenguanli.vue').catch(handleLazyLoadError('yishengguanli')),
      },
      {
        path: '/guanliyuan/gonggaoguanli',
        name: 'gonggaoguanli',
        component: () => import('../views/guanliyuan/gonggaoguanli.vue').catch(handleLazyLoadError('gonggaoguanli')),
      },
      {
        path: '/guanliyuan/lunbotuguanli',
        name: 'lunbotuguanli',
        component: () => import('../views/guanliyuan/lunbotuguanli.vue').catch(handleLazyLoadError('glunbotuguanli')),
      },
      {
        path: '/guanliyuan/gerenzhongxin',
        name: 'gerenzhongxin',
        component: () => import('../views/guanliyuan/gerenzhongxin.vue').catch(handleLazyLoadError('gerenzhongxin')),
      },
  
    ]
  },
  {
    path: '/yisheng/home',
    name: 'DoctorHome',
    component: () => import('../views/yisheng/yishengHome.vue').catch(handleLazyLoadError('DoctorHome')),
    meta: { title: '医生首页', requiresAuth: true, role: 'doctor' },
    children: [
      {
        path: '/yisheng/ho',
        name: 'doctorInfo',
        component: () => import('../views/yisheng/ho.vue').catch(handleLazyLoadError('doctorInfo')),
      },
      {
        path: '/yisheng/gaimima',
        name: 'doctorChangePwd',
        component: () => import('../views/yisheng/yishenggaimima.vue').catch(handleLazyLoadError('doctorChangePwd')),
      },
      {
        path: '/yisheng/jiezhen',
        name: 'doctorjiezhen',
        component: () => import('../views/yisheng/yishengjiezhen.vue').catch(handleLazyLoadError('doctorjiezhen')),
      },
      {
        path: '/yisheng/keshi',
        name: 'doctorkeshi',
        component: () => import('../views/yisheng/yishengkeshi.vue').catch(handleLazyLoadError('doctorkeshi')),
      },
      {
        path: '/yisheng/paiban',
        name: 'doctorpaiban',
        component: () => import('../views/yisheng/yishengpaiban.vue').catch(handleLazyLoadError('doctorpaiban')),
      }
    ]
  },
  {
    path: '/yonghu/home',
    name: 'PatientHome',
    component: () => import('../views/yonghu/yonghuHome.vue').catch(handleLazyLoadError('PatientHome')),
    meta: { title: '病人首页', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/my',
    name: 'my',
    component: () => import('@/views/yonghu/My.vue').catch(handleLazyLoadError('My')),
    meta: { title: '我的', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/hospital-info',
    name: 'HospitalInfo',
    component: () => import('@/views/yonghu/HospitalInfo.vue').catch(handleLazyLoadError('HospitalInfo')),
    meta: { title: '医院信息', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/my-appointments',
    name: 'MyAppointments',
    component: () => import('@/views/yonghu/MyAppointments.vue').catch(handleLazyLoadError('MyAppointments')),
    meta: { title: '我的预约', requiresAuth: true, role: 'patient' }
  },
  {
    path: '/yonghu/online-registration',
    name: 'OnlineRegistration',
    component: () => import('@/views/yonghu/OnlineRegistration.vue').catch(handleLazyLoadError('OnlineRegistration')),
    meta: { title: '在线挂号', requiresAuth: true, role: 'patient' }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

export default router;